package com.collect.cameroon.zhiScene.zhiLogin;

import android.content.Context;

import com.collect.cameroon.zhiBase.BasePresenter;
import com.collect.cameroon.zhiBase.BaseSubscriber;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRAppVersionEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRCheckInOutEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRLoginEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRUserCallRecordWarnEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISLoginEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISResetPasswordEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISendEntity;
import com.collect.cameroon.zhiHelper.ZHIMainProcessHelper;
import com.collect.cameroon.zhiNetApi.ZHIHttpApiProvider;
import com.collect.cameroon.zhiNetApi.ZHIHttpResponse;
import com.collect.cameroon.zhiUtils.RxSchedulers;

import javax.inject.Inject;

public class ZHILoginPresenter extends BasePresenter<ZHILoginContact.View> implements ZHILoginContact.Presenter {
    @Inject
    public ZHILoginPresenter() {

    }

    @Override
    public void getLoginInfoV2(Context context, final ZHISLoginEntity ftsLoginEntity) {

        String methodName = "";
        StackTraceElement[] s = Thread.currentThread().getStackTrace();
        if (s.length >= 2) {
            methodName = s[2].getMethodName();
        }
        String finalMethodName = methodName;

        BaseSubscriber<ZHIHttpResponse<ZHIRLoginEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRLoginEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRLoginEntity zhiRLoginEntity = (ZHIRLoginEntity) object;
                            ZHIMainProcessHelper.getInstance().loginV67(zhiRLoginEntity, ftsLoginEntity.getLoginName(), ftsLoginEntity.getPassWord());
                            mView.onGetLoginInfoV2(zhiRLoginEntity);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        try {
                            showMessage(messge);
                            mView.onGetErrorInfo(finalMethodName);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
        ZHIHttpApiProvider.getApi().userLoginV2(ftsLoginEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getUserCallRecordWarn(Context context, ZHISendEntity zhiSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRUserCallRecordWarnEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRUserCallRecordWarnEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        ZHIRUserCallRecordWarnEntity zhirUserCallRecordWarnEntity = (ZHIRUserCallRecordWarnEntity) object;
                        if (mView != null) {
                            mView.onGetUserCallRecordWarn(zhirUserCallRecordWarnEntity);
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo("getUserCallRecordWarn");
                    }
                };
        ZHIHttpApiProvider.getApi().getUserCallRecordWarn(zhiSendEntity)
                .compose(RxSchedulers.<ZHIHttpResponse<ZHIRUserCallRecordWarnEntity>>applySchedulers())
                .compose(mView.<ZHIHttpResponse<ZHIRUserCallRecordWarnEntity>>bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void resetPassword(Context context, ZHISResetPasswordEntity zhisResetPasswordEntity) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        if (mView != null) {
                            mView.onResetPassword();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo("resetPassword");
                    }
                };
        ZHIHttpApiProvider.getApi().resetPassword(zhisResetPasswordEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getAppVersionInfoV2(Context context, ZHISendEntity ftSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRAppVersionEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRAppVersionEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        ZHIRAppVersionEntity ftrAppVersionEntity = (ZHIRAppVersionEntity) object;
                        if (mView != null) {
                            mView.onGetAppVersionInfoV2(ftrAppVersionEntity);
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo("");
                    }
                };
        ZHIHttpApiProvider.getApi().getAppVersionInfoV2(ftSendEntity)
                .compose(RxSchedulers.<ZHIHttpResponse<ZHIRAppVersionEntity>>applySchedulers())
                .compose(mView.<ZHIHttpResponse<ZHIRAppVersionEntity>>bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getCheckState(Context context, ZHISendEntity zhiSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRCheckInOutEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRCheckInOutEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRCheckInOutEntity zhirCheckInOutEntity = (ZHIRCheckInOutEntity) object;
                            ZHIMainProcessHelper.getInstance().setZHIRCheckInOutEntity(zhirCheckInOutEntity);
                            mView.onGetCheckState(zhirCheckInOutEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo("getCheckState");
                    }
                };
        ZHIHttpApiProvider.getApi().checkInOutState(zhiSendEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

}
